home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / CPAN / FirstTime.pm next >
Encoding:
Perl POD Document  |  1999-12-28  |  10.6 KB  |  380 lines

  1. package CPAN::Mirrored::By;
  2.  
  3. sub new { 
  4.     my($self,@arg) = @_;
  5.     bless [@arg], $self;
  6. }
  7. sub continent { shift->[0] }
  8. sub country { shift->[1] }
  9. sub url { shift->[2] }
  10.  
  11. package CPAN::FirstTime;
  12.  
  13. use strict;
  14. use ExtUtils::MakeMaker qw(prompt);
  15. use FileHandle ();
  16. use File::Path ();
  17. use vars qw($VERSION);
  18. $VERSION = substr q$Revision: 1.20 $, 10;
  19.  
  20. =head1 NAME
  21.  
  22. CPAN::FirstTime - Utility for CPAN::Config file Initialization
  23.  
  24. =head1 SYNOPSIS
  25.  
  26. CPAN::FirstTime::init()
  27.  
  28. =head1 DESCRIPTION
  29.  
  30. The init routine asks a few questions and writes a CPAN::Config
  31. file. Nothing special.
  32.  
  33. =cut
  34.  
  35.  
  36. sub init {
  37.     my($configpm) = @_;
  38.     use Config;
  39.     require CPAN::Nox;
  40.     eval {require CPAN::Config;};
  41.     $CPAN::Config ||= {};
  42.     local($/) = "\n";
  43.     local($\) = "";
  44.  
  45.     my($ans,$default,$local,$cont,$url,$expected_size);
  46.     
  47.  
  48.     print qq{
  49. The CPAN module needs a directory of its own to cache important
  50. index files and maybe keep a temporary mirror of CPAN files. This may
  51. be a site-wide directory or a personal directory.
  52. };
  53.  
  54.     my $cpan_home = $CPAN::Config->{cpan_home} || MM->catdir($ENV{HOME}, ".cpan");
  55.     if (-d $cpan_home) {
  56.     print qq{
  57.  
  58. I see you already have a  directory
  59.     $cpan_home
  60. Shall we use it as the general CPAN build and cache directory?
  61.  
  62. };
  63.     } else {
  64.     print qq{
  65.  
  66. First of all, I\'d like to create this directory. Where?
  67.  
  68. };
  69.     }
  70.  
  71.     $default = $cpan_home;
  72.     while ($ans = prompt("CPAN build and cache directory?",$default)) {
  73.     File::Path::mkpath($ans); # dies if it can't
  74.     if (-d $ans && -w _) {
  75.         last;
  76.     } else {
  77.         warn "Couldn't find directory $ans
  78.   or directory is not writable. Please retry.\n";
  79.     }
  80.     }
  81.     $CPAN::Config->{cpan_home} = $ans;
  82.     
  83.     print qq{
  84.  
  85. If you want, I can keep the source files after a build in the cpan
  86. home directory. If you choose so then future builds will take the
  87. files from there. If you don\'t want to keep them, answer 0 to the
  88. next question.
  89.  
  90. };
  91.  
  92.     $CPAN::Config->{keep_source_where} = MM->catdir($CPAN::Config->{cpan_home},"sources");
  93.     $CPAN::Config->{build_dir} = MM->catdir($CPAN::Config->{cpan_home},"build");
  94.  
  95.  
  96.     print qq{
  97.  
  98. How big should the disk cache be for keeping the build directories
  99. with all the intermediate files?
  100.  
  101. };
  102.  
  103.     $default = $CPAN::Config->{build_cache} || 10;
  104.     $ans = prompt("Cache size for build directory (in MB)?", $default);
  105.     $CPAN::Config->{build_cache} = $ans;
  106.  
  107.     $CPAN::Config->{'index_expire'} = 1;
  108.  
  109.  
  110.     print qq{
  111.  
  112. The CPAN module will need a few external programs to work
  113. properly. Please correct me, if I guess the wrong path for a program.
  114. Don\'t panic if you do not have some of them, just press ENTER for
  115. those.
  116.  
  117. };
  118.  
  119.     my(@path) = split /$Config{'path_sep'}/, $ENV{'PATH'};
  120.     my $prog;
  121.     for $prog (qw/gzip tar unzip make lynx ncftp ftp/){
  122.     my $path = $CPAN::Config->{$prog} || "";
  123.     if (MM->file_name_is_absolute($path)) {
  124.         warn "Warning: configured $path does not exist\n" unless -e $path;
  125.         $path = "";
  126.     } else {
  127.         $path = '';
  128.     }
  129.     $path ||= find_exe($prog,[@path]);
  130.     warn "Warning: $prog not found in PATH\n" unless -e $path;
  131.     $ans = prompt("Where is your $prog program?",$path) || $path;
  132.     $CPAN::Config->{$prog} = $ans;
  133.     }
  134.     my $path = $CPAN::Config->{'pager'} || 
  135.     $ENV{PAGER} || find_exe("less",[@path]) || 
  136.         find_exe("more",[@path]) || "more";
  137.     $ans = prompt("What is your favorite pager program?",$path);
  138.     $CPAN::Config->{'pager'} = $ans;
  139.     $path = $CPAN::Config->{'shell'};
  140.     if (MM->file_name_is_absolute($path)) {
  141.     warn "Warning: configured $path does not exist\n" unless -e $path;
  142.     $path = "";
  143.     }
  144.     $path ||= $ENV{SHELL};
  145.     $ans = prompt("What is your favorite shell?",$path);
  146.     $CPAN::Config->{'shell'} = $ans;
  147.  
  148.  
  149.     print qq{
  150.  
  151. Every Makefile.PL is run by perl in a separate process. Likewise we
  152. run \'make\' and \'make install\' in processes. If you have any parameters
  153. \(e.g. PREFIX, INSTALLPRIVLIB, UNINST or the like\) you want to pass to
  154. the calls, please specify them here.
  155.  
  156. If you don\'t understand this question, just press ENTER.
  157.  
  158. };
  159.  
  160.     $default = $CPAN::Config->{makepl_arg} || "";
  161.     $CPAN::Config->{makepl_arg} =
  162.     prompt("Parameters for the 'perl Makefile.PL' command?",$default);
  163.     $default = $CPAN::Config->{make_arg} || "";
  164.     $CPAN::Config->{make_arg} = prompt("Parameters for the 'make' command?",$default);
  165.  
  166.     $default = $CPAN::Config->{make_install_arg} || $CPAN::Config->{make_arg} || "";
  167.     $CPAN::Config->{make_install_arg} =
  168.     prompt("Parameters for the 'make install' command?",$default);
  169.  
  170.  
  171.     print qq{
  172.  
  173. Sometimes you may wish to leave the processes run by CPAN alone
  174. without caring about them. As sometimes the Makefile.PL contains
  175. question you\'re expected to answer, you can set a timer that will
  176. kill a 'perl Makefile.PL' process after the specified time in seconds.
  177.  
  178. If you set this value to 0, these processes will wait forever. This is
  179. the default and recommended setting.
  180.  
  181. };
  182.  
  183.     $default = $CPAN::Config->{inactivity_timeout} || 0;
  184.     $CPAN::Config->{inactivity_timeout} =
  185.     prompt("Timeout for inacivity during Makefile.PL?",$default);
  186.  
  187.  
  188.  
  189.     $local = 'MIRRORED.BY';
  190.     $local = MM->catfile($CPAN::Config->{keep_source_where},"MIRRORED.BY") unless -f $local;
  191.     if (@{$CPAN::Config->{urllist}||[]}) {
  192.     print qq{
  193. I found a list of URLs in CPAN::Config and will use this.
  194. You can change it later with the 'o conf' command.
  195.  
  196. }
  197.     } elsif (
  198.          -s $local
  199.          &&
  200.          -M $local < 30
  201.         ) {
  202.     read_mirrored_by($local);
  203.     } else {
  204.     $CPAN::Config->{urllist} ||= [];
  205.     while (! @{$CPAN::Config->{urllist}}) {
  206.         my($input) = prompt(qq{
  207. We need to know the URL of your favorite CPAN site.
  208. Please enter it here:});
  209.         $input =~ s/\s//g;
  210.         next unless $input;
  211.         my($wanted) = "MIRRORED.BY";
  212.         print qq{
  213. Testing "$input" ...
  214. };
  215.         push @{$CPAN::Config->{urllist}}, $input;
  216.         CPAN::FTP->localize($wanted,$local,"force");
  217.         if (-s $local) {
  218.         print qq{
  219. "$input" seems to work
  220. };
  221.         } else {
  222.         my $ans = prompt(qq{$input doesn\'t seem to work. Keep it in the list?},"n");
  223.         last unless $ans =~ /^n/i;
  224.         pop @{$CPAN::Config->{urllist}};
  225.         }
  226.     }
  227.     }
  228.  
  229.     unless (@{$CPAN::Config->{'wait_list'}||[]}) {
  230.     print qq{
  231.  
  232. WAIT support is available as a Plugin. You need the CPAN::WAIT module
  233. to actually use it.  But we need to know your favorite WAIT server. If
  234. you don\'t know a WAIT server near you, just press ENTER.
  235.  
  236. };
  237.     $default = "wait://ls6.informatik.uni-dortmund.de:1404";
  238.     $ans = prompt("Your favorite WAIT server?\n  ",$default);
  239.     push @{$CPAN::Config->{'wait_list'}}, $ans;
  240.     }
  241.  
  242.     print qq{
  243.  
  244. If you\'re accessing the net via proxies, you can specify them in the
  245. CPAN configuration or via environment variables. The variable in
  246. the \$CPAN::Config takes precedence.
  247.  
  248. };
  249.  
  250.     for (qw/ftp_proxy http_proxy no_proxy/) {
  251.     $default = $CPAN::Config->{$_} || $ENV{$_};
  252.     $CPAN::Config->{$_} = prompt("Your $_?",$default);
  253.     }
  254.  
  255.     $CPAN::Config->{'inhibit_startup_message'} = 0;
  256.     $CPAN::Config->{'getcwd'} = 'cwd';
  257.  
  258.     print "\n\n";
  259.     CPAN::Config->commit($configpm);
  260. }
  261.  
  262. sub find_exe {
  263.     my($exe,$path) = @_;
  264.     my($dir);
  265.     for $dir (@$path) {
  266.     my $abs = MM->catfile($dir,$exe);
  267.     if (MM->maybe_command($abs)) {
  268.         return $abs;
  269.     }
  270.     }
  271. }
  272.  
  273. sub read_mirrored_by {
  274.     my($local) = @_;
  275.     my(%all,$url,$expected_size,$default,$ans,$host,$dst,$country,$continent,@location);
  276.     my $fh = FileHandle->new;
  277.     $fh->open($local) or die "Couldn't open $local: $!";
  278.     while (<$fh>) {
  279.     ($host) = /^([\w\.\-]+)/ unless defined $host;
  280.     next unless defined $host;
  281.     next unless /\s+dst_(dst|location)/;
  282.     /location\s+=\s+\"([^\"]+)/ and @location = (split /\s*,\s*/, $1) and
  283.         ($continent, $country) = @location[-1,-2];
  284.     $continent =~ s/\s\(.*//;
  285.     /dst_dst\s+=\s+\"([^\"]+)/  and $dst = $1;
  286.     next unless $host && $dst && $continent && $country;
  287.     $all{$continent}{$country}{$dst} = CPAN::Mirrored::By->new($continent,$country,$dst);
  288.     undef $host;
  289.     $dst=$continent=$country="";
  290.     }
  291.     $fh->close;
  292.     $CPAN::Config->{urllist} ||= [];
  293.     if ($expected_size = @{$CPAN::Config->{urllist}}) {
  294.     for $url (@{$CPAN::Config->{urllist}}) {
  295.         next unless $url =~ /^\w+:\/./;
  296.         $all{"[From previous setup]"}{"found URL"}{$url}=CPAN::Mirrored::By->new('[From previous setup]','found URL',$url);
  297.     }
  298.     $CPAN::Config->{urllist} = [];
  299.     } else {
  300.     $expected_size = 6;
  301.     }
  302.     
  303.     print qq{
  304.  
  305. Now we need to know, where your favorite CPAN sites are located. Push
  306. a few sites onto the array (just in case the first on the array won\'t
  307. work). If you are mirroring CPAN to your local workstation, specify a
  308. file: URL.
  309.  
  310. You can enter the number in front of the URL on the next screen, a
  311. file:, ftp: or http: URL, or "q" to finish selecting.
  312.  
  313. };
  314.  
  315.     $ans = prompt("Press RETURN to continue");
  316.     my $other;
  317.     $ans = $other = "";
  318.     my(%seen);
  319.     
  320.     my $pipe = -t *STDIN ? "| $CPAN::Config->{'pager'}" : ">/dev/null";
  321.     while () {
  322.     my(@valid,$previous_best);
  323.     my $fh = FileHandle->new;
  324.     $fh->open($pipe);
  325.     {
  326.         my($cont,$country,$url,$item);
  327.         my(@cont) = sort keys %all;
  328.         for $cont (@cont) {
  329.         $fh->print("    $cont\n");
  330.         for $country (sort {lc $a cmp lc $b} keys %{$all{$cont}}) {
  331.             for $url (sort {lc $a cmp lc $b} keys %{$all{$cont}{$country}}) {
  332.             my $t = sprintf(
  333.                     "      %-18s (%2d) %s\n",
  334.                     $country,
  335.                     ++$item,
  336.                     $url
  337.                        );
  338.             if ($cont =~ /^\[/) {
  339.                 $previous_best ||= $item;
  340.             }
  341.             push @valid, $all{$cont}{$country}{$url};
  342.             $fh->print($t);
  343.             }
  344.         }
  345.         }
  346.     }
  347.     $fh->close;
  348.     $previous_best ||= 1;
  349.     $default =
  350.         @{$CPAN::Config->{urllist}} >= $expected_size ? "q" : $previous_best;
  351.     $ans = prompt(
  352.               "\nSelect an$other ftp or file URL or a number (q to finish)",
  353.               $default
  354.              );
  355.     my $sel;
  356.     if ($ans =~ /^\d/) {
  357.         my $this = $valid[$ans-1];
  358.         my($con,$cou,$url) = ($this->continent,$this->country,$this->url);
  359.         push @{$CPAN::Config->{urllist}}, $url unless $seen{$url}++;
  360.         delete $all{$con}{$cou}{$url};
  361.     } elsif (@{$CPAN::Config->{urllist}} && $ans =~ /^q/i) {
  362.         last;
  363.     } else {
  364.         $ans =~ s|/?$|/|; # has to end with one slash
  365.         $ans = "file:$ans" unless $ans =~ /:/; # without a scheme is a file:
  366.         if ($ans =~ /^\w+:\/./) {
  367.         push @{$CPAN::Config->{urllist}}, $ans unless $seen{$ans}++;
  368.         } else {
  369.         print qq{"$ans" doesn\'t look like an URL at first sight.
  370. I\'ll ignore it for now. You can add it to lib/CPAN/Config.pm
  371. later and report a bug in my Makefile.PL to me (andreas koenig).
  372. Thanks.\n};
  373.         }
  374.     }
  375.     $other ||= "other";
  376.     }
  377. }
  378.  
  379. 1;
  380.